package com.survey.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.survey.common.core.domain.entity.SysUser;
import com.survey.common.utils.DateUtils;
import com.survey.common.utils.SecurityUtils;
import com.survey.system.domain.SurFeedback;
import com.survey.system.mapper.SurFeedbackMapper;
import com.survey.system.mapper.SysDictDataMapper;
import com.survey.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.survey.system.mapper.SurIssueMapper;
import com.survey.system.domain.SurIssue;
import com.survey.system.service.ISurIssueService;

/**
 * 问题信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-23
 */
@Service
public class SurIssueServiceImpl implements ISurIssueService 
{
    @Autowired
    private SurIssueMapper surIssueMapper;
    @Autowired
    private SurFeedbackMapper surFeedbackMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询问题信息
     * 
     * @param issueId 问题信息主键
     * @return 问题信息
     */
    @Override
    public SurIssue selectSurIssueByIssueId(Long issueId)
    {
        return surIssueMapper.selectSurIssueByIssueId(issueId);
    }

    /**
     * 查询问题信息列表
     * 
     * @param surIssue 问题信息
     * @return 问题信息
     */
    @Override
    public List<SurIssue> selectSurIssueList(SurIssue surIssue)
    {
        List<SurIssue> surIssues = surIssueMapper.selectSurIssueList(surIssue);
        for (SurIssue surIssue1 : surIssues) {
            String issueStatus = sysDictDataMapper.selectDictLabel("sys_issue_status", surIssue1.getIssueStatus().toString());
            surIssue1.setIssueStatusName(issueStatus);
        }
        return surIssues;
    }

    /**
     * 新增问题信息
     * 
     * @param surIssue 问题信息
     * @return 结果
     */
    @Override
    public int insertSurIssue(SurIssue surIssue)
    {
        surIssue.setIssueStatus(0L);
        return surIssueMapper.insertSurIssue(surIssue);
    }

    /**
     * 修改问题信息
     * 
     * @param surIssue 问题信息
     * @return 结果
     */
    @Override
    public int updateSurIssue(SurIssue surIssue)
    {
        surIssue.setUpdateTime(DateUtils.getNowDate());
        return surIssueMapper.updateSurIssue(surIssue);
    }

    /**
     * 批量删除问题信息
     * 
     * @param issueIds 需要删除的问题信息主键
     * @return 结果
     */
    @Override
    public int deleteSurIssueByIssueIds(Long[] issueIds)
    {
        return surIssueMapper.deleteSurIssueByIssueIds(issueIds);
    }

    /**
     * 删除问题信息信息
     * 
     * @param issueId 问题信息主键
     * @return 结果
     */
    @Override
    public int deleteSurIssueByIssueId(Long issueId)
    {
        return surIssueMapper.deleteSurIssueByIssueId(issueId);
    }

    @Override
    public SurFeedback selectSurFeedbackByIssueId(Long issueId) {
        return surFeedbackMapper.selectSurFeedbackByIssueId(issueId);
    }

    @Override
    public List<Integer> countByDate() {
        List<Integer> numList = new ArrayList<>();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        // 获取今天的数据（今天的日期格式化为字符串）
        String today = dateFormat.format(date);
        int countToday = surIssueMapper.countByDate(today);
        numList.add(countToday);  // 添加今天的数据

        // 获取过去6天的数据
        for (int i = 1; i <= 6; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);  // 每次回退1天
            Date time = calendar.getTime();
            String formattedDate = dateFormat.format(time);
            int count = surIssueMapper.countByDate(formattedDate);
            numList.add(count);
        }

        return numList;
    }

    @Override
    public Integer selectTodaySurIssue() {
        return surIssueMapper.selectTodaySurIssue();
    }

}
